package org.example.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.example.entity.Post;
import org.example.models.R;
import org.example.service.PostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/system/permission/post")
public class PostController {
    @Autowired
    private PostService postService;

    // 分页条件查询
    @GetMapping
    public R<Page<Post>> page(
            @RequestParam(name = "current", defaultValue = "1") int current,
            @RequestParam(name = "size", defaultValue = "5") int size,
            @RequestParam(name = "postName", required = false) String postName,
            @RequestParam(name = "postCode", required = false) String postCode,
            @RequestParam(name = "status", required = false) Integer status
    ) {
        QueryWrapper<Post> query = new QueryWrapper<>();
        if (postName != null && postName.length() > 0) {
            query.lambda().like(Post::getPostName, postName);
        }
        if (postCode != null && postCode.length() > 0) {
            query.lambda().like(Post::getPostCode, postCode);
        }
        if (status != null) {
            query.lambda().eq(Post::getStatus, status);
        }
        query.lambda().orderByAsc(Post::getPostSort);
        Page<Post> page = new Page<>(current, size);
        page = postService.page(page, query);
        return R.success(page);
    }

    // 新增
    @PostMapping("/insert")
    public R<String> save(@RequestBody Post post) {
        boolean result = postService.save(post);
        if (result) {
            return R.success("添加成功");
        } else {
            return R.error("添加失败");
        }
    }

    // 修改
    @PutMapping("/update")
    public R<String> update(@RequestBody Post post) {
        boolean result = postService.updateById(post);
        if (result) {
            return R.success("修改成功");
        } else {
            return R.error("修改失败");
        }
    }

    // 删除
    @DeleteMapping("{id}")
    public R<String> delete(@PathVariable Integer id) {
        boolean result = postService.removeById(id);
        if (result) {
            return R.success("删除成功");
        } else {
            return R.error("删除失败");
        }
    }

    @GetMapping("/find")
    public R<List<Post>> findAll() {
        QueryWrapper<Post> queryWrapper = new QueryWrapper<>();
        List<Post> empList = postService.list(queryWrapper);
        return R.success(empList);
    }

    @GetMapping("/finds")
    public R<List<Post>> finds(
            @RequestParam(name = "postId" ,required = false) Integer postId
    ){
        QueryWrapper<Post> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(Post::getPostId,postId);
        List<Post> empList = postService.list(queryWrapper);
        return R.success(empList);
    }


    // 为岗位调动模块添加的接口
    @GetMapping("/list")
    public R<List<Post>> list() {
        try {
            QueryWrapper<Post> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("status", 1); // 只获取启用的岗位
            List<Post> postList = postService.list(queryWrapper);
            return R.success(postList);
        } catch (Exception e) {
            e.printStackTrace();
            return R.error("获取岗位列表失败: " + e.getMessage());
        }
    }
}

